
/**
** \file       case_item.c
**
** \brief      this function parse the case_item rule
**
** \author     Ben M'rad Hanna, Laterem Hamza, Boillot Ulysse
**             and De Olivera Guillaume
**
** \version    1.0
**
** \date       28 Novembre 2010
**
*/

#include "rules.h"

static s_caseitem               *pipe_star(s_caseitem        *citem)
{
  s_word                        *tmp_word = NULL;

  tmp_word = citem->word;

  while ((g_data->lexer != NULL) && (!strcmp(g_data->lexer->token->id, "|")))
  {
    if ((g_data->lexer = g_data->lexer->next) != NULL
        && g_data->lexer->token->type == TOKEN_WORD)
    {
      tmp_word->word = calloc(1, (strlen(g_data->lexer->token->id) + 1));
      tmp_word->next = calloc(1, sizeof (s_word));
      g_data->lexer = g_data->lexer->next;
    }

    if (g_data->lexer != NULL && strcmp(g_data->lexer->token->id, ")") == 0)
      g_data->lexer = g_data->lexer->next;
    else
    {
      free(tmp_word->word);
      tmp_word->word = NULL;
      free(tmp_word->next);
      tmp_word->next = NULL;

      return (NULL);
    }

    tmp_word = tmp_word->next;
  }

  return (citem);
}

s_caseitem              *rule_case_item(s_caseitem        *citem)
{
  if (g_data->lexer != NULL && strcmp(g_data->lexer->token->id, "(") == 0)
    g_data->lexer = g_data->lexer->next;

  if (g_data->lexer != NULL && g_data->lexer->token->type == TOKEN_WORD)
  {
    citem->word = calloc(1, sizeof (s_word));
    citem->word->word = calloc(1, (strlen(g_data->lexer->token->id) + 1));
    citem->word->word = strcpy(citem->word->word, g_data->lexer->token->id);
    g_data->lexer = g_data->lexer->next;
  }

  if (g_data->lexer == NULL)
  {
    free_word(citem->word);
    citem->word = NULL;
    return (NULL);
  }

  pipe_star(citem);

  if (g_data->lexer != NULL && strcmp(g_data->lexer->token->id, ")") == 0)
    g_data->lexer = g_data->lexer->next;

  citem->cmpdlist = calloc(1, sizeof (s_cmpdlist));

  if (rule_compound_list(citem->cmpdlist) == NULL)
  {
    free(citem->cmpdlist);
    citem->cmpdlist = NULL;

    return (NULL);
  }

  return (citem);
}
